
local mt = ac.buff['击飞']


mt.debuff = true
mt.maxheight = 1024   --- 最高叠加到的高度
mt.control = true
mt.level = 1
    
mt.height = 512       --- 击飞高度
mt.time = 1.00        --- 击飞时间

mt.pulse = 0.03
mt.physics = true     --- 物理Buff

mt.stun_flag = true

function mt:on_add()
    local unit = self.owner
    if self:get_remaining() <= 0 then 
        self:remove()
        return false 
    end
    self.default = unit:get_default_height()
    self.current = unit:get_height()
    self.point = unit:get_point()
    self.highest = (self.height + self.current) / (1 + (self.height + self.current) / self.maxheight)
    self.length = (self.height + self.highest - self.default)
    if self.length <= 0 then 
        self:remove()
        return false
    end
    self.speed = self.length / self.time
    self.progress = 0.0
    self.moved = 0.0
    self.has_unmove = true
    self.has_resume = true
    self.has_launch = true
    unit:notify("单位-打断位移", unit)
    unit:add_restriction "位移"
    unit:add_restriction "幽灵"
    if self.stun_flag then 
        self.has_stun = true
        unit:add_restriction "晕眩"
    end
    self.trigger = unit:on "单位-打断位移" (function (_, _, flag)
        self.has_resume = (flag == true) 
        self:notify("on_break")
        self:remove()
    end)
    self:notify("on_launch")
end

function mt:on_pulse()
    local unit = self.owner 
    self.moved = self.moved + self.speed * self.pulse
    self.progress = self.moved / self.length 
    unit:set_height( math.parabola(self.current, self.highest, self.default, self.progress) )
end

function mt:on_remove()
    local unit = self.owner 
    if self.has_unmove then 
        unit:remove_restriction "位移"
        unit:remove_restriction "幽灵"
        if self.has_stun then 
            unit:remove_restriction "晕眩"
        end
    end
    if self.has_resume then 
        unit:set_height( unit:get_default_height() )
    end 
    if self.trigger then 
        self.trigger:remove()
    end
    if self.has_launch then 
        self:notify("on_stop")
    end 
end

function mt:on_cover(new)
    local time = self:get_remaining()
    if new.time < time then 
        new.time = time
    end 
    new.maxheight = math.max(self.maxheight, new.maxheight)
    self.has_resume = false
    return true
end
